package com.mantou.mantouaiagent.rag.document.writer.vectorstore;

import com.mantou.mantouaiagent.rag.document.reader.LoveAppDocumentLoader;
import com.mantou.mantouaiagent.rag.document.transformer.MyKeywordEnricher;
import com.mantou.mantouaiagent.rag.document.transformer.MyTokenTextSplitter;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;


import java.util.List;

@Configuration
@Slf4j
public class LoveAppVectorStoreConfig {


    @Resource
    private LoveAppDocumentLoader loveAppDocumentLoader;

    @Resource
    private MyTokenTextSplitter myTokenTextSplitter;

    @Resource
    private MyKeywordEnricher myKeywordEnricher;


//    @Bean
//    VectorStore loveAppVectorStore(EmbeddingModel dashscopeEmbeddingModel) {
//        SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(dashscopeEmbeddingModel)
//                .build();
//        // 加载文档
//        List<Document> documents = loveAppDocumentLoader.loadMarkdowns();
//
//        //手动文档分片
//        List<Document> splitDocuments = myTokenTextSplitter.splitDocuments(documents);
//
//        //自动补充添加关键词元数据
//        List<Document> enrichDocuments = myKeywordEnricher.enrichDocuments(splitDocuments);
//
//        simpleVectorStore.add(enrichDocuments);
//
//        return simpleVectorStore;
//    }
    //构造器注入
    private final EmbeddingModel embeddingModel;
    private final VectorStore loveAppVectorStore;

    // 构造器注入EmbeddingModel
    public LoveAppVectorStoreConfig(EmbeddingModel dashscopeEmbeddingModel) {
        this.embeddingModel = dashscopeEmbeddingModel;
        this.loveAppVectorStore = createVectorStore();
    }

    // 通过@Bean方法暴露VectorStore Bean
    @Bean
    public VectorStore loveAppVectorStore() {
        return this.loveAppVectorStore;
    }

    private VectorStore createVectorStore() {
        return SimpleVectorStore.builder(embeddingModel)
                .build();
    }

    // 异步初始化方法
    @Async
    @EventListener(ApplicationReadyEvent.class)
    public void asyncVectorStoreInit(ApplicationReadyEvent event) {
        try {
            List<Document> documents = loveAppDocumentLoader.loadMarkdowns();
            List<Document> splitDocuments = myTokenTextSplitter.splitDocuments(documents);
            List<Document> enrichDocuments = myKeywordEnricher.enrichDocuments(splitDocuments);

            // 使用构造器注入的VectorStore实例
            loveAppVectorStore.add(enrichDocuments);

            log.info("向量库初始化完成，成功添加{}个文档", enrichDocuments.size());
        } catch (Exception e) {
            log.error("向量库初始化失败", e);
            throw new RuntimeException("VectorStore初始化失败", e);
        }
    }
}
